package jdbc;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

/**
 * 查询account账户表的内容,封装为Account对象中,并且存储到ArrayList中
 * 1.定义Account类
 * 2.查询数据,封装到Account类中
 * 3.存入ArrayList中
 */
public class Demo {
    public static void main(String[] args) {

    }
    @Test
    public void testResultSet2() throws Exception{
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql:///db1?useSSL=false";
        String userName = "root";
        String password = "123456";
        Connection con = DriverManager.getConnection(url,userName,password);

        String sql = "select id,name,money from account";
        Statement stmt = con.createStatement();
        ArrayList<Account> al = new ArrayList<>();

        ResultSet rs = stmt.executeQuery(sql);
        //最开始rs的光标是指在表头的,我们需要逐行判断
        while(rs.next()){
            int id = rs.getInt("id");
            String name = rs.getString("name");
            double money = rs.getDouble("money");

            al.add(new Account(id,name,money));

            System.out.println(id + name + money);
        }

        for (int i = 0; i < al.size(); i++) {
            System.out.println(al.get(i));
        }

        //7.释放资源
        rs.close();
        stmt.close();
        con.close();
    }
}
